package org.teemor.demo.zookeeper.simple;

import ch.qos.logback.core.util.CloseUtil;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.utils.CloseableUtils;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.teemor.demo.zookeeper.factory.ClientFactory;

import java.io.Closeable;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Description:
 * @author zhoulk
 * @date: 2020/2/19
 */
public class SimpleDemo {

    private String connectionString = "common.teemor.xyz:2181";

    public static void main(String[] args) {
        SimpleDemo simpleDemo = new SimpleDemo();
        simpleDemo.createNode();
    }

    public void createNode() {
        CuratorFramework client = ClientFactory.createSimple("common.teemor.xyz:2181");
        client.start();
        String data = "hello";
        try {
            byte[] payload = data.getBytes("UTF-8");
            String zkPath = "/test/CRUD/node-";
            for (int i = 0; i < 10; i++) {
                Object o = client.create()
                        .creatingParentsIfNeeded()
                        .withMode(CreateMode.PERSISTENT_SEQUENTIAL)//创建序列节点，会自动在node- 后面加上序号
                        .forPath(zkPath, payload);
                System.out.println(o.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CloseableUtils.closeQuietly(client);
        }
    }

    public void readNode() {
        CuratorFramework client = ClientFactory.createSimple(connectionString);
        try {
            client.start();
            String zkPath = "/test/CRUD/node-1";
            Stat stat = client.checkExists().forPath(zkPath);
            if (null != stat) {
                byte [] payload = client.getData().forPath(zkPath);
                String data = new String(payload, "UTF-8");
                System.out.println("nodeDate:" + data);
                String parentPath = "/test/CRUD";
                List<String> chirldrens = client.getChildren().forPath(parentPath);
                for (String chirldren : chirldrens) {
                    System.out.println("chirldren:" + chirldren);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CloseableUtils.closeQuietly(client);
        }
    }

    public void updateNode() {
        CuratorFramework client = ClientFactory.createSimple(connectionString);
        try {
           client.start();
           String data = "hello world";
           byte [] payload = data.getBytes("UTF-8");
           String path = "/test/CRUD/node-1";
           client.setData().forPath(path, payload);
        } catch (Exception e) {
            close(client);
        }
    }

    public void deleteNode() {
        CuratorFramework client = ClientFactory.createSimple(connectionString);
        try {
            client.start();
            String path = "/test/CRUD/node-1";
            client.delete().forPath(path);
        } catch (Exception e) {
            close(client);
        }
    }

    private void close(CuratorFramework client) {
        CloseableUtils.closeQuietly(client);

    }
}
